option compress=yes;
proc datasets lib=work nolist kill; run; quit;
DM "log; clear; ";
DM "odsresults; clear";

/* library */
libname taq "This is the path for just downloaded turnover";
libname link "This is the path for the link file between crsp and taq";
libname crsp "This is the path for crsp";


*============================================================================;
*step 1.1: NASDAQ, after 2015
*============================================================================;

data nasdaq_after2015; 
 set taq.nasdaq2015to18 taq.nasdaq2019to21;
run;

proc sql;
 create table nasdaq_after2015_1 as
 select a.*, b.permno
 from nasdaq_after2015 as a left join link.taqmclink_cusip_2010 as b
 on a.cusip = b.cusip and a.date >= b.date1 and a.date <= b.date2
 where permno is not missing
 order by a.date, a.symbol, a.time
;
quit;

proc sql;
 create table nasdaq_after2015_2 as
 select a.*, b.SHRCD, b.EXCHCD, (b.SHROUT*b.CFACSHR) as SHROUT_ADJ
 from nasdaq_after2015_1 as a left join crsp.nasdaq_shrout as b
 on a.permno = b.permno and a.date = b.date
 where calculated shrout_adj is not missing
 order by a.date, a.symbol, a.time
;
quit; 

data nasdaq_after2015_3; set nasdaq_after2015_2; where shrout_adj ne 0;
 turnover = volume/(shrout_adj*1000);
run;


*============================================================================;
*step 1.2: NASDAQ, before 2015
*============================================================================;

data nasdaq_before2014; 
 set taq.nasdaq1993to95 taq.nasdaq1996to96 taq.nasdaq1997to99 taq.nasdaq2000to01 taq.nasdaq2002to0406
     taq.nasdaq2004to0412 taq.nasdaq2005to05 taq.nasdaq2006to07 taq.nasdaq2008to09 taq.nasdaq2010to11
	 taq.nasdaq2012to13 taq.nasdaq2014to14
;
run;

proc sql;
 create table nasdaq_before2014_1 as
 select a.*, b.permno
 from nasdaq_before2014 as a left join link.tclink as b
 on a.cusip = b.cusip_full and year(a.date) = year(b.date) and month(a.date) = month(b.date)
 where permno is not missing and a.date >= b.fdate
 order by a.date, a.symbol, a.time
;
quit;

proc sql;
 create table nasdaq_before2014_2 as
 select a.*, b.SHRCD, b.EXCHCD, (b.SHROUT*b.CFACSHR) as SHROUT_ADJ
 from nasdaq_before2014_1 as a left join crsp.nasdaq_shrout as b
 on a.permno = b.permno and a.date = b.date
 where calculated shrout_adj is not missing
 order by a.date, a.symbol, a.time
;
quit;

data nasdaq_before2014_3; set nasdaq_before2014_2; where shrout_adj ne 0;
 turnover = volume/(shrout_adj*1000);
run;


*============================================================================;
*step 1.3: NASDAQ, merge before and after 2015
*============================================================================;

data nasdaq_turnover; set nasdaq_before2014_3 nasdaq_after2015_3; run; 

proc means data = nasdaq_turnover N NMISS MEAN STD MIN P1 P99 MAX; var turnover; run;

/* NASDAQ has multiple identifiers, sum across them for each stock */
proc sql;
 create table nasdaq_turnover_1 as
 select distinct permno, date, time, sum(turnover) as turnover
 from nasdaq_turnover
 group by permno, date, time
 order by permno, date, time
;
quit;

/* average across firm */
proc sort data = nasdaq_turnover_1; by permno time date; run;
proc means data= nasdaq_turnover_1 noprint;
by date time;
where shrcd in (10 11);
var turnover;
output out = _nasdaq (drop=_TYPE_ _FREQ_) N = obs MEAN=turnover;
run;

/* average across dates */
proc sort data = _nasdaq; by time; run;
proc means data= _nasdaq noprint;
by time;
where turnover < 2;
var turnover;
output out = _nasdaq_out (drop=_TYPE_ _FREQ_) N = obs MEAN=turnover;
run;
